package org.admanager.soa.plugin.popup.actions;

import org.admanager.soa.plugin.util.KermetaRunner;
import org.admanager.soa.plugin.util.ModifyXMLFile;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.wizards.datatransfer.FileSystemImportWizard;

import fr.irisa.triskell.eclipse.console.EclipseConsole;
import fr.irisa.triskell.eclipse.console.IOConsole;

public class SCAModelImportActionDelegate implements IObjectActionDelegate {
	private ISelection selection;
	private IWorkbenchPart targetPart;

	public SCAModelImportActionDelegate() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run(IAction action) {
		// TODO Auto-generated method stub
		if (selection instanceof IStructuredSelection) {

			FileSystemImportWizard wizard = new FileSystemImportWizard();
			wizard.init(null, (IStructuredSelection) selection);

			// wizard.in, selection);
			WizardDialog dialog = new WizardDialog(targetPart.getSite()
					.getShell(), wizard);
			dialog.open();

			Object SCAModelFolder = ((IStructuredSelection) selection)
					.getFirstElement();
			if (SCAModelFolder instanceof IFolder) {
				IFolder folder = (IFolder) SCAModelFolder;
				try {
					for (int i = 0; i < folder.members().length; i++) {
						if (folder.members()[i] instanceof IFile) {
							IFile file = (IFile) folder.members()[i];
							String str = file.getFileExtension();
							if (file.getFileExtension().equals("composite")) {
								IFile newFile = folder.getFile(file.getName()
										.replace(".composite", ".sca"));
								
								newFile.create(file.getContents(true), false,
										null);								
								
								ModifyXMLFile.modify(newFile.getLocationURI().toString());
								
								file.delete(true, null);
								folder.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
								//ResourcesPlugin.getWorkspace().getRoot().getProjects()
								//Run Kermeta
								IOConsole console = new EclipseConsole("Kermeta Simulator");
								try {			
									URI patternURI = URI.createURI(newFile.getLocationURI()
											.toString());
									
									String sourcePatternUrl = patternURI.toString();
									String targetPatternUrl = patternURI.toString().replace(".sca", ".simplifiedsca");
									
									KermetaRunner.SCA2SimplifiedSCA(sourcePatternUrl, targetPatternUrl, console);
									
								} catch (Throwable e) {
									
									e.printStackTrace();
								}
								//End Run Kermeta
								
								newFile.delete(true, null);
								folder.refreshLocal(IResource.DEPTH_INFINITE, null);
							}
						}
					}
				} catch (CoreException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		this.selection = selection;
		action.setEnabled(!selection.isEmpty());
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		this.targetPart = targetPart;
	}

}
